//
// Created by goldfish on 30/3/25.
//
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <wchar.h>
#include <locale.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sstream>

using namespace std;

// 配置参数
struct Config {
    string eyes = "oo";
    string tongue = "  ";
    string cowfile = "default";
    int width = 40;
    bool wrap = true;
    bool list = false;
    bool think = false;
    bool dead = false;
    bool borg = false;
    // 其他标志位...
};

// 全局配置
Config config;
vector<string> cowpaths = {"./cows", "/usr/share/cowsay/cows"};

// 工具函数
int mbwidth(const string& s) {
    setlocale(LC_ALL, "");
    int w = 0;
    mbstate_t state = {};
    const char* ptr = s.c_str();
    const char* end = ptr + s.size();

    while(ptr < end) {
        wchar_t wc;
        size_t n = mbrtowc(&wc, ptr, end - ptr, &state);
        if(n == (size_t)-1 || n == (size_t)-2) break;
        ptr += n;
        w += wcwidth(wc);
    }
    return w;
}

vector<string> wrap_text(const string& text, int width) {
    vector<string> lines;
    stringstream ss(text);
    string word, line;

    while(ss >> word) {
        if(line.empty()) {
            line = word;
        } else if (mbwidth(line) + mbwidth(word) + 1 <= width) {
            line += " " + word;
        } else {
            lines.push_back(line);
            line = word;
        }
    }
    if(!line.empty()) lines.push_back(line);
    return lines;
}

// 主要功能实现
vector<string> build_balloon(const vector<string>& lines) {
    int max_len = 0;
    for(const auto& line : lines) {
        max_len = max(max_len, mbwidth(line));
    }

    vector<string> balloon;
    string top = " " + string(max_len + 2, '_');
    string bottom = " " + string(max_len + 2, '-');

    balloon.push_back(top);

    if(lines.size() == 1) {
        balloon.push_back("< " + lines[0] + " >");
    } else {
        balloon.push_back("/ " + string(max_len, ' ') + " \\");
        for(size_t i=1; i<lines.size()-1; ++i) {
            balloon.push_back("| " + lines[i] + string(max_len - mbwidth(lines[i]), ' ') + " |");
        }
        balloon.push_back("\\ " + lines.back() + string(max_len - mbwidth(lines.back()), ' ') + " /");
    }

    balloon.push_back(bottom);
    return balloon;
}

string get_cow() {
    // 这里可以加载cow文件内容，示例使用简单奶牛
    return R"(
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
    )";
}

// 命令行参数解析
void parse_args(int argc, char* argv[]) {
    static struct option long_options[] = {
        {"eyes",    required_argument, 0, 'e'},
        {"tongue",  required_argument, 0, 'T'},
        {"file",    required_argument, 0, 'f'},
        {"width",   required_argument, 0, 'W'},
        {"list",    no_argument,       0, 'l'},
        {"think",   no_argument,       0, 't'},
        {"dead",    no_argument,       0, 'd'},
        {0, 0, 0, 0}
    };

    int c;
    while((c = getopt_long(argc, argv, "e:T:f:W:ltd", long_options, NULL)) != -1) {
        switch(c) {
            case 'e': config.eyes = optarg; break;
            case 'T': config.tongue = optarg; break;
            case 'f': config.cowfile = optarg; break;
            case 'W': config.width = stoi(optarg); break;
            case 'l': config.list = true; break;
            case 't': config.think = true; break;
            case 'd': config.dead = true; break;
            // 处理其他选项...
        }
    }
}

int main(int argc, char* argv[]) {
    parse_args(argc, argv);

    // 读取输入消息
    vector<string> message;
    if(optind < argc) {
        for(int i=optind; i<argc; i++) {
            message.emplace_back(argv[i]);
        }
    } else {
        string line;
        while(getline(cin, line)) {
            message.push_back(line);
        }
    }

    // 处理消息换行
    vector<string> wrapped;
    for(const auto& line : message) {
        auto parts = wrap_text(line, config.width);
        wrapped.insert(wrapped.end(), parts.begin(), parts.end());
    }

    // 生成对话框
    auto balloon = build_balloon(wrapped);

    // 生成奶牛
    string cow = get_cow();

    // 输出结果
    for(const auto& line : balloon) {
        cout << line << endl;
    }
    cout << cow << endl;

    return 0;
}